<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="payal";
        $this->tablename="paypal_account";  
    }
        
    public function getpage($page,$key=""){    
        $where=[];
     
        if($key)
        {
            $key=["account"=>$key];
        }
        return $this->get_items_by_page($where,$page,"backend/paypal/account","id desc",[],$key);                
    }

    public function getall(){        
        $where=[];
        return $this->get_items_by_where($where);                
    }

    public function getacounts($siteid)
    {
        $accid=[];
        $query=$this->db->where(["siteid=".$siteid])->order_by("id desc")->get($this->table_prefix."paypal_bind");                
        $items=$query->result_array();
        if($items){
            foreach($items as $item)
            {
                $accid[]=$item["accid"];
            }
            return $this->getitemsbyid($accid);         
        }else{
            return false;
        }
    }

    public function getaccountmap($accid)
    {
        $query=$this->db->where(["accid"=>$accid])->order_by("id desc")->get($this->table_prefix."paypal_bind");                
        $items=$query->result_array();
        if($items){
            return $items;
        }else{
            return false;
        }
    }

    public function bind($siteid,$accid)
    {
        $query=$this->db->where(["siteid"=>$siteid,"accid"=>$accid])->get($this->table_prefix."paypal_bind");
        $item=$query->row_array();
        if($item){
            return $item;
        }else{
            $newitem=["siteid"=>$siteid,"accid"=>$accid,"addtime"=>date("Y-m-d H:i:s")];
            $this->db->insert($this->table_prefix."paypal_bind", $newitem);
            $id = $this->db->insert_id();
            if($id){
                $newitem["id"]=$id;
                return $newitem;
            }
            else
            {
                return false;
            }
        }
    }

    public function dellink($id)
    {
        $this->db->where(["id"=>$id]);
        $this->db->delete($this->table_prefix."paypal_bind");
        return true;
    }
   
    public function delete($id)
    {        
        return true;
    }

    public function getitemsbyid($id)
    {
        $where="status=1 and id in(".implode(",",$id).")";
        return $this->get_items_by_where($where);
    }

    public function getitembyid($id)
    {
        $where=array("id"=>$id);
        return $this->get_item_by_where($where);
    }

    public function getnormalaccount($siteid)
    {
        $list = $this->get_items_by_where(["status"=>1]);
        return $list;
    }

    public function get_name_index()
    {
        $index=[];
        $sites=$this->getall();
        if($sites)
        {   
            foreach($sites as $site)
            {
                $index[$site["id"]]=$site["account"];
            }

        }

        return $index;
    }
    public function create($data){      
        return $this->add_item($data);       
    }

    public function summary($accid,$amount)
    {
        $account=$this->getitembyid($accid);
        if($account)
        {
            $newdata["succcount"]=$account["succcount"]+1;
            $newdata["succtotal"]=$account["succtotal"]+$amount;
            if($newdata["succcount"]>$account["limitcount"] || $newdata["succtotal"]>$account["limitamount"])
            {
                $newdata["status"]=3;
            }
            $this->update($accid,$newdata);            
        }
    }

    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
           
        }else{
            $where=array("id"=>$id);
           
        }
        return $this->update_item($where,$data);

    }

   
}
